package com.aster.service.manage.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import com.aster.common.annotation.Log;
import com.aster.common.base.BaseController;
import com.aster.common.base.BaseDomain;
import com.aster.common.dto.UserToken;
import com.aster.common.exception.ServiceException;
import com.aster.common.utils.PageUtils;
import com.aster.common.utils.Query;
import com.aster.common.utils.R;
import com.aster.service.manage.domain.TbEscortEnterpriseInfo;
import com.aster.service.manage.domain.TbLabelInfo;
import com.aster.service.manage.dto.DtoEscortEnterpriseInfo;
import com.aster.service.manage.dto.DtoLabelInfo;
import com.aster.service.manage.dto.do2dto.EscortEnterpriseInfoConvert;
import com.aster.service.manage.dto.do2dto.LabelInfoConvert;
import com.aster.service.manage.service.LabelInfoService;
import com.aster.service.manage.service.criteria.EscortEnterpriseInfoCriteria;
import com.aster.service.manage.service.criteria.LabelInfoCriteria;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 
 * <br>
 * <b>功能：</b>TbLabelInfoController<br>
 *   <br>
 */
@RestController
@RequestMapping("/labelInfo")
public class LabelInfoController extends BaseController {
	
	private final static Logger log= Logger.getLogger(LabelInfoController.class);
	@Autowired(required=false) 
	private LabelInfoService tbLabelInfoService;

	@Log("标签列表")
	@GetMapping
	public R listByPage(@RequestParam Map<String, Object> params, HttpServletRequest headers){
		try {
			UserToken userToken = getUserToken(headers);
			Query query = new Query(params);
			LabelInfoCriteria criteria = new LabelInfoCriteria();
			criteria.setPageSize(query.getLimit());
			criteria.setCurrentPage(query.getPage());
			Map<String,Object> likeMap = new HashMap<>();
			likeMap.put("labelText",query.get("labelText"));
			criteria.setConditionLike(likeMap);
			Map<String,Object> map = new HashMap<>();
			map.put("deleted",1);
			map.put("enterpriseId",userToken.getEnterpriseId());
			criteria.setCondition(map);
			criteria.setOrderByClause("id");
			PageUtils pageUtils = tbLabelInfoService.getLabelInfoForPage(criteria);
			return R.ok().put("page",pageUtils);
		} catch (ServiceException e) {
			e.printStackTrace();
			return R.error(e.getMessage());
		}
	}

	@Log("获取全部标签信息")
	@GetMapping("/getList")
	public R getList(){
		try{
			LabelInfoCriteria criteria = new LabelInfoCriteria();
			Map<String,Object> map = new HashMap<>();
			map.put("deleted",1);
			map.put("state",0);
			criteria.setCondition(map);
			criteria.setOrderByClause("id");
			criteria.setCurrentPage(1);
			Integer totalCount = tbLabelInfoService.queryByCount(criteria);
			criteria.setPageSize(totalCount);
			List<TbLabelInfo> list = tbLabelInfoService.queryByList(criteria);
			List<DtoLabelInfo> dtoList = LabelInfoConvert.MAPPER.dos2dtos(list);
			return R.ok().put("info",dtoList);
		}catch (ServiceException ex){
			return R.error(ex.getMessage());
		}catch (NumberFormatException ex){
			return R.error(ex.getMessage());
		}
		catch (Exception ex){
			return R.error(ex.getMessage());
		}
	}

	@Log("根据id获取标签信息")
	@RequestMapping("/getById")
	public R getById(@RequestParam Map<String, Object> params){
		try{
			String idStr = (String) params.get("id");
			int id = Integer.parseInt(idStr);
			TbLabelInfo info = tbLabelInfoService.queryById(id);
			DtoLabelInfo dtoLabelInfo = LabelInfoConvert.MAPPER.do2dto(info);
			return R.ok().put("info",dtoLabelInfo);
		}catch (ServiceException ex){
			return R.error(ex.getMessage());
		}catch (NumberFormatException ex){
			return R.error(ex.getMessage());
		}
		catch (Exception ex){
			return R.error(ex.getMessage());
		}
	}

	@Log("添加标签信息")
	@PostMapping("/add")
	public R add(@RequestBody DtoLabelInfo dtoLabelInfo, HttpServletRequest headers){
		try {
			UserToken userToken = getUserToken(headers);
			if(userToken == null){
				return R.error401();
			}
			TbLabelInfo tbLabelInfo = LabelInfoConvert.MAPPER.addData(dtoLabelInfo);
			tbLabelInfoService.addLabelInfo(tbLabelInfo,userToken);
			return R.ok();
		}
		catch (ServiceException ex){
			return R.error(ex.getMessage());
		}
		catch (Exception ex){
			return R.error(ex.getMessage());
		}
	}

	@Log("编辑标签信息")
	@PostMapping("/edit")
	public R editState(@RequestBody DtoLabelInfo dtoLabelInfo , HttpServletRequest headers){
		try {
			UserToken userToken = getUserToken(headers);
			TbLabelInfo tbLabelInfo = LabelInfoConvert.MAPPER.dto2do(dtoLabelInfo);
			tbLabelInfoService.editLabelInfo(tbLabelInfo,userToken);
			return R.ok();
		}catch (ServiceException ex){
			return R.error(ex.getMessage());
		}
		catch (Exception ex){
			return R.error(ex.getMessage());
		}
	}

	@Log("删除标签信息")
	@PostMapping("/delete")
	public R delete(@RequestBody DtoLabelInfo dtoLabelInfo ,HttpServletRequest headers){
		try{
			if(dtoLabelInfo == null){
				return R.error(10001,"传入参数为空");
			}
			if(dtoLabelInfo.getId() == 0){
				return R.error(10001,"传入参数为空");
			}
			UserToken userToken = getUserToken(headers);
			if(userToken == null){
				return R.error401();
			}
			dtoLabelInfo.setDeleted(BaseDomain.DELETED.NO.key);
			TbLabelInfo tbLabelInfo = LabelInfoConvert.MAPPER.updateDeleted(dtoLabelInfo);
			tbLabelInfoService.editLabelInfo(tbLabelInfo,userToken);
			return R.ok();
		}catch (ServiceException ex){
			return R.error(ex.getMessage());
		}
		catch (Exception ex){
			return R.error(ex.getMessage());
		}
	}

	@Log("修改标签数据状态")
	@PostMapping("/changeState")
	public R changeState(@RequestBody DtoLabelInfo dtoLabelInfo,HttpServletRequest headers){
		try {
			if(dtoLabelInfo == null){
				return R.error(10001,"传入参数为空");
			}
			if(dtoLabelInfo.getId() == 0){
				return R.error(10001,"传入参数为空");
			}
			UserToken userToken = getUserToken(headers);
			if(userToken == null){
				return R.error401();
			}
			TbLabelInfo tbLabelInfo = LabelInfoConvert.MAPPER.updateState(dtoLabelInfo);
			tbLabelInfoService.editLabelInfo(tbLabelInfo,userToken);
			return R.ok();
		}catch (ServiceException ex){
			return R.error(ex.getMessage());
		}
		catch (Exception ex){
			return R.error(ex.getMessage());
		}
	}
}
